<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="科目码">
              <a-input placeholder="请输入..." v-model="queryParam.kmbm"/>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="科目名称">
              <a-input placeholder="请输入..." v-model="queryParam.kmmc"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
          <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="院系">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.ssyxm" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="科目类型">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.kmlxm">
                <a-select-option v-for="i in TYPE0390" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          </template>
          <a-col :sm="{span:16}" :xl="{span:8}" style="padding-top: 29px;">
            <a-button type="primary" @click="loadData">查询</a-button>
            <a-button type="primary" @click="exportExcel">导出</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :loading="loading"
      :dataSource="data"
      :alert="options.alert"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      :pagination="pagination"
      :scroll="{ x: 1000 }"
      @change="handleTableChange"
    >


      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
    </a-table>
  </a-card>
</template>

<script>
// import moment from 'moment'

import { STable } from '~'
import { mapState } from 'vuex'
import { constants } from 'fs';

const selectArr = ['TYPE0390']

export default {
  components: {
    STable
  },
  data () {
    return {
      advanced: true,
      loading: false,
      data: [],
      // 查询参数
      queryParam: {
        kmbm:'',
        kmlxm:'',
        kmmc:'',
        ssyxm:''
      },
      postParams: {},
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' },
          width:80
        },
        {
          title: '科目码',
          dataIndex: 'kmbm',
          width:100
        },
        {
          title: '科目名称',
          dataIndex: 'kmmc',
          width:200
        },
        {
          title: '科目类型',
          dataIndex: 'kmlxm',
          width:100
        },
        {
          title: '所属院系',
          dataIndex: 'ssyxm'
        }
      ],

      // custom table alert & rowSelection
      options: {
        alert: { show: false }
      },
      preViewVisible: false,
      referenceManageAddVisiable: false,
      referenceManageEditVisiable: false,
      selectMenu: {
        yx: [] // 院系
      },
      pagination: {
					current: 1
				},
      referenceManageEditData: {},
      referenceManageViewData: {},
      selectedRows:'',
       selectedRowKeys: [],
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,

      TYPE0390: state => state.app['TYPE0390'] // 年级
    })
  },
  created () {
    this.initSelectMenu()

    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
          this.queryParam.ssyxm=res[0].data[0].dwh
         this.loadData({
          pageSize: 10,
          pageNum: 1
        })
      })
    },
    onSelectChange (selectedRowKeys,selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows=selectedRows;
    },
    toEdit (data) {
      this.$router.push(`/doctorRecruit/admission/${data.id}/referenceDetail`)
    },
    // 选择院系
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      this.$set(this.selectMenu,'zy','')
      if(code){
      this.$api.base.majorAll({lsdwh: code}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
      }
    },
     // 加载数据
    loadData(parameter = {}) {
      if(!parameter.pageNum){
         this.pagination.current = 1
         parameter.pageNum = 1
       }
      let params = Object.assign(this.queryParam, parameter)
      this.loading = true;
      this.$api.doctorRecruit.consultList(params).then(res => {
        // console.log(res)
         const pagination = { ...this.pagination
        };
        pagination.total = 0
        if (res.data && res.data.total) {
          pagination.total = res.data.total;
        }
        this.data = []
        this.pagination = pagination;
        if (res.data && res.data.rows.length) {
          this.data = res.data.rows;
          this.TYPE0390.forEach(item=>{
            this.data.forEach(_item=>{
              if(_item.kmlxm==item.code){
                _item.kmlxm =item.name;
              }
            })
          })
            //  if(this.data.ssyxm){
              this.data.map(item=>{
                if(item.ssyxm){

                  item.ss=item.ssyxm.split(',')

                  item.newss= item.ss.map(_item=>{

                   this.selectMenu.yx.map(key=>{
                        if(key.dwh==_item){
                          _item=key.dwmc
                          // item.sn.push(key.dwmc)

                        }
                      })
                    return _item
                   })
                   item.ssyxm=item.newss.join(',')
                }
                return item
              })
        }
        this.loading = false;
      })
    },
        // 分页
			handleTableChange(pagination, filters, sorter) {
				console.log(pagination);
				const pager = { ...this.pagination
				};
				pager.current = pagination.current;
				this.pagination = pager;
				this.loadData({
					pageSize: 10,
					pageNum: pagination.current
				});
			},
    // 选择招生年度
    zsndChange () {

    },
    // 添加
    handleCreate () {
      this.referenceManageAddVisiable = true
    },
    // 关闭添加弹层
    handleCreateClose () {
      this.referenceManageAddVisiable = false
    },
    // 添加成功
    handleCreateSuccess () {
      this.referenceManageAddVisiable = false
      this.$message.success('添加成功')
      this.loadData({
              pageNum:this.pagination.current,
              pageSize:10
            })
    },
    // 修改
    handleEdit (key) {
      this.$api.doctorRecruit.consultId(key).then(res=>{
        if(res.data){
        this.referenceManageEditData=res.data;
        }
      })
      this.referenceManageEditVisiable = true
    },
    // 关闭添加弹层
    handleEditClose () {
      this.referenceManageEditVisiable = false
    },
    // 添加成功
    handleEditSuccess () {
      this.referenceManageEditVisiable = false
      this.$message.success('修改成功')
      this.loadData({
              pageNum:this.pagination.current,
              pageSize:10
            })
    },
    // 导出
    exportExcel () {
      console.log('导出')
       this.$export('admissions/zsBsckkm/export',this.queryParam)
    },
    // 导入
    importExcel () {
      console.log('导出')

    },
    // 删除
    handleDelete (key) {
      let _this=this;
      this.$confirm({
         title: '确定删除?',
         onOk() {
           _this.$api.doctorRecruit.consultDelete(key).then(res=>{
          if(res.status==1){
            _this.loadData({
              pageNum:_this.pagination.current,
              pageSize:10
            })
          }
      })
         },
         onCancel() {
           console.log('Cancel');
         },
         class: 'test',
       });

    },
    // 批量删除
    handleBatchDelete () {
      let _this=this;
        this.$confirm({
         title: '确定删除?',
         onOk() {
            if (!_this.selectedRowKeys.length) {
        _this.$message.warning('请选择需要报到的学生')
        return
      }
      let para=[],params
      _this.selectedRows.forEach(item=>{
        para.push(item.id);
      })
      params=para.join(',')
      _this.$api.doctorRecruit.consultDelete(params).then(res=>{
        if(res.status==1){
          _this.loadData({
              pageNum:_this.pagination.current,
              pageSize:10
            })
          _this.selectedRowKeys = [];
        }
      })
         },
         onCancel() {
           console.log('Cancel');
         },
         class: 'test',
       });

    },
    handlePreView (data) {
       this.$api.doctorRecruit.consultId(data).then(res=>{
        if(res.data){
        this.referenceManageViewData=res.data;
        }
      })
      this.preViewVisible = true

    },
    handlePreViewClose () {
      this.preViewVisible = false
    }
  }
}
</script>
